package com.ws.universal.tools.headimgview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ImageView;

/**
 * @创建者 ws
 * @TIME 2021/8/16 17:21
 * @描述 自定义类型属性，0是圆形，1是矩形圆角
 */
@SuppressLint("AppCompatCustomView")
public class HeadImageView extends ImageView {
    //圆形
    public static final int TYPE_CIRCLE = 0;
    //圆角
    public static final int TYPE_ROUND = 1;

    private static final String STATE_INSTANCE = "state_instance";
    private static final String STATE_TYPE = "state_type";
    private static final String STATE_BORDER_RADIUS = "state_border_radius";
    // 图片的类型，圆形or圆角
    private int mode;
    //圆角的大小
    private int mBorderRadius;
    // 绘图的Paint
    private Paint mBitmapPaint;
    //圆角的半径
    private int mRadius;
    // 3x3 矩阵，主要用于缩小放大
    private Matrix mMatrix;
    // 渲染图像，使用图像为绘制图形着色
    private BitmapShader mBitmapShader;
    // view的宽度
    private int mWidth;
    //矩形
    private RectF mRoundRect;

    public HeadImageView(Context context) {
        this(context, null);
    }

    public HeadImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMatrix = new Matrix();
        mBitmapPaint = new Paint();
        mBitmapPaint.setAntiAlias(true);


        mode = 0;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 如果类型是圆形，则强制改变view的宽高一致，以小值为准
        if (mode == TYPE_CIRCLE) {
            mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());
            //圆形的半径
            mRadius = mWidth / 2;
            setMeasuredDimension(mWidth, mWidth);
        }

    }

    // 初始化BitmapShader,获取到图片资源
    // 等待画布的准备好，然后在画布上加上Paint就是了
    //就是说图片的载体是Paint
    private void setUpShader() {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }

        Bitmap bmp = drawableToBitamp(drawable);
        // 将bmp作为着色器，就是在指定区域内绘制bmp
        //TileMode.CLAMP 拉伸
        mBitmapShader = new BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        float scale = 1.0f;
        // 拿到bitmap宽或高的小值
        int bSize = Math.min(bmp.getWidth(), bmp.getHeight());
        scale = mWidth * 1.0f / bSize;
        // shader的变换矩阵，我们这里主要用于放大或者缩小
        // scale * scale 的矩阵
        mMatrix.setScale(scale, scale);
        // 设置变换矩阵
        mBitmapShader.setLocalMatrix(mMatrix);
        // 设置shader
        mBitmapPaint.setShader(mBitmapShader);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (getDrawable() == null) {
            return;
        }
        setUpShader();

        //绘制圆形
        canvas.drawCircle(mRadius, mRadius, mRadius, mBitmapPaint);
    }


    //drawable转bitmap
    private Bitmap drawableToBitamp(Drawable drawable) {
        //从控件的src获取背景，也是drawable文件获取
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            return bd.getBitmap();
        }

        //如果没有绘图一个，只不过是空白的图片
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();

        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        drawable.draw(canvas);
        return bitmap;
    }

    //屏幕旋转后，取出保存的值
    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putParcelable(STATE_INSTANCE, super.onSaveInstanceState());
        bundle.putInt(STATE_TYPE, mode);
        bundle.putInt(STATE_BORDER_RADIUS, mBorderRadius);
        return bundle;
    }

    //屏幕旋转，变量的保存，因为外面设置值，如果不保存，一旋转就变成个xml里面设置的值
    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle) {
            Bundle bundle = (Bundle) state;
            super.onRestoreInstanceState(((Bundle) state).getParcelable(STATE_INSTANCE));
            this.mode = bundle.getInt(STATE_TYPE);
            this.mBorderRadius = bundle.getInt(STATE_BORDER_RADIUS);
        } else {
            super.onRestoreInstanceState(state);
        }

    }

    //设置矩形圆角幅度后，重新绘制控件
    public void setBorderRadius(int borderRadius) {
        int pxVal = dp2px(borderRadius);
        if (this.mBorderRadius != pxVal) {
            this.mBorderRadius = pxVal;
            invalidate();
        }
    }


    //dp转px
    public int dp2px(int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());
    }

}
